<?php

class __photos_mail extends Module {
	
	public function __construct () {
		parent::__construct ('photos');
	}
	
	public function photoReplyNotification (Object $oPhoto, Object $oComment) {
		$typesController = TypesController::getInstance ();
		$objectsController = ObjectsController::getInstance ();
		
		if (ExplaySession::getUserId() == $oPhoto->getValue ('owner_id')) {
			return false;
		}
		
		$oPhotoAuthor = $objectsController->getObject ($typesController->getTypeByName ('user'), $oPhoto->getValue ('owner_id'));
		
		$oTypeSettings = $typesController->getTypeByName ('user_settings');
		$selUserSettings = new Selection;
		$selUserSettings->addType ($oTypeSettings);
		$selUserSettings->addFilter ('user_id', $oPhotoAuthor->getId ());
		$selUserSettings->addLimit (1);
		
		$res = $selUserSettings->getList ();
		if (count ($res)) {
			$oSettings = $objectsController->getObject ($oTypeSettings, $res[0][0]);
			if ($oSettings->getValue ('notification_postcomment') == 0) {
				return false;
			}
		}
		
		$oCurrentUser = ExplaySession::getUser ();
		
		$photoTitle = $oPhoto->getValue ('title') == '' ? lang('without_name','photos') : $oPhoto->getValue ('title');
		
		$template = new SimpleTemplate ('photos/mailPhotoReply.tpl');
		$template->setVar ('{photoOwnerName}', $oPhotoAuthor->getValue ('name'));
		$template->setVar ('{photoOwnerLogin}', $oPhotoAuthor->getValue ('login'));
		$template->setVar ('{commentOwnerLogin}', $oCurrentUser->getValue ('login'));
		$template->setVar ('{commentOwnerName}', $oCurrentUser->getValue ('name'));
		$template->setVar ('{commentId}', $oComment->getId ());
		$template->setVar ('{commentText}', $oComment->getValue ('text'));
		$template->setVar ('{photoId}', $oPhoto->getId ());
		$template->setVar ('{photoTitle}', $photoTitle);
		
		Mailer::sendEMail(new EMail ($oPhotoAuthor->getValue ('email'), $template->display (), lang('email_photo_reply','photos') . '"' . $photoTitle . '"'));
	}
	
	public function commentReplyNotification (Object $oPhoto, Object $oRelComment, Object $oReplyComment) {
		$typesController = TypesController::getInstance ();
		$oTypeComment = $typesController->getTypeByName ('comment');
		$objectsController = ObjectsController::getInstance ();
		
		if (ExplaySession::getUserId() == $oRelComment->getValue ('owner_id')) {
			return false;
		}
		
		$oCommentAuthor = $objectsController->getObject ($typesController->getTypeByName ('user'), $oRelComment->getValue ('owner_id'));
		
		$oTypeSettings = $typesController->getTypeByName ('user_settings');
		$selUserSettings = new Selection;
		$selUserSettings->addType ($oTypeSettings);
		$selUserSettings->addFilter ('user_id', $oCommentAuthor->getId ());
		$selUserSettings->addLimit (1);
		
		$res = $selUserSettings->getList ();
		if (count ($res)) {
			$oSettings = $objectsController->getObject ($oTypeSettings, $res[0][0]);
			if ($oSettings->getValue ('notification_comment') == 0) {
				return false;
			}
		}
		
		$oCurrentUser = ExplaySession::getUser ();
		
		$template = new SimpleTemplate ('photos/mailCommentReply.tpl');
		$template->setVar ('{relCommentOwnerLogin}', $oCommentAuthor->getValue ('login'));
		$template->setVar ('{relCommentOwnerName}', $oCommentAuthor->getValue ('name'));
		$template->setVar ('{replyCommentOwnerLogin}', $oCurrentUser->getValue ('login'));
		$template->setVar ('{replyCommentOwnerName}', $oCurrentUser->getValue ('name'));
		$template->setVar ('{relCommentId}', $oRelComment->getId ());
		$template->setVar ('{replyCommentId}', $oReplyComment->getId ());
		$template->setVar ('{commentText}', $oReplyComment->getValue ('text'));
		$template->setVar ('{photoId}', $oPhoto->getId ());
		$template->setVar ('{photoTitle}', $oPhoto->getValue ('title'));
		
		Mailer::sendEMail(new EMail ($oCommentAuthor->getValue ('email'), $template->display (), lang('email_comment_reply','photos')));
	}
	
}